#include "stdafx.h"
#include "MouseChecker.h"
#include "RectSelect.h"

MouseChecker::MouseChecker()
:m_bDisableCheck(FALSE)
,m_pCurSelect(NULL)
,m_pMouseDown(NULL)
{}

MouseChecker::~MouseChecker()
{}

void MouseChecker::AddSelectCheck(RectSelect* pRect)
{
	m_listSelect.push_back(pRect);
}

void MouseChecker::ReleaseSelectCheck(RectSelect* pRect)
{
	std::vector<RectSelect* >::iterator itr = find(m_listSelect.begin(), m_listSelect.end(), pRect);
	if( itr != m_listSelect.end() )
	{
		m_listSelect.erase(itr);
	}
}

void MouseChecker::CheckSelectRect(int nPosX, int nPosY)
{
	if( m_bDisableCheck )	return;

	m_pCurSelect = NULL;

	std::vector<RectSelect* >::iterator itr = m_listSelect.begin();
	while( itr != m_listSelect.end() )
	{
		(*itr)->SetSelect(FALSE);	
		++itr;
	}

	itr = m_listSelect.begin();
	while( itr != m_listSelect.end() )
	{
		if( (*itr)->PtInRect(nPosX, nPosY) )
		{
			(*itr)->SetSelect(TRUE);
			m_pCurSelect = (*itr);
			break;
		}
		++itr;
	}
}

RectSelect* MouseChecker::MouseDownSelectRect()
{
	if( m_pCurSelect )	
	{
		m_pCurSelect->SetClickDown();
		m_pMouseDown = m_pCurSelect;
		return m_pCurSelect;
	}

	return NULL;
}

RectSelect* MouseChecker::MouseUpSelectRect()
{
	if( m_pMouseDown )	
	{
		m_pMouseDown->SetClickUp();
		RectSelect* pTemp = m_pMouseDown;
		m_pMouseDown = NULL;
		return pTemp;
	}

	return NULL;
}

void MouseChecker::Clear()
{
	m_listSelect.clear();
	m_pCurSelect = NULL;
	m_pMouseDown = NULL;
}

